//后台接口
'use strict'

const Service = require('egg').Service

class adminService extends Service{
    async checkLoginService(params){
        try {
            const {app} = this
            let userName = params.userName
            let password = params.password
    
            let sql = "SELECT userName FROM admin_user WHERE userName= '" + userName +"' AND password = '"+ password +"'"

            const res = await app.mysql.query(sql)
            return res
        } catch (error) {
            console.log(error)
            return null
        }
    }
    //获取类型信息
    async getTypeInfoService(){
        try {
            const resType = await this.app.mysql.select('type')
            // console.log(resType)
            return resType
        } catch (error) {
            console.log(error)
            return null
        }
    }
    //添加文章
    async addArticleService(tmpArticle){
        try {
            const resulte = await this.app.mysql.insert('article',tmpArticle)
            return resulte
        } catch (error) {
            console.log(error)
            return null
        }
    }

    //修改文章
    async updateActicleService(update){
        try {
            const  app = this.app
            const result = await app.mysql.update('article',update)
            return result
        } catch (error) {
            console.log(error)
            return null
        }
    }

    //查看文章列表
    async getArticleListService(){
        try {
            const app = this.app
            let sql = 'SELECT article.id as id,'+
            'article.title as title,'+
            'article.introduce as introduce,'+
            'article.view_count as view_count,'+
            'article.article_content as content,'+
            "FROM_UNIXTIME(article.addTime,'%Y-%m-%d' ) as addTime,"+
            'type.typeName as typeName '+
            'FROM article LEFT JOIN type ON article.type_id = type.Id '+
            'ORDER BY article.id DESC '

            const result =  await app.mysql.query(sql)
            return result
        } catch (error) {
            console.log(error)
            return null
            
        }
    }

    //删除文章
    async delArticleService(id){
        try {
            const app = this.app
            const result = await app.mysql.delete('article',{'id':id})
            return result
        } catch (error) {
            console.log(error)
            return null            
        }
    }

    //修改文章
    async editArticleService(id){
        try{
            const app = this.app
            let sql = 'SELECT article.id as id,'+
                'article.title as title,'+
                'article.introduce as introduce,'+
                'article.article_content as article_content,'+
                "FROM_UNIXTIME(article.addTime,'%Y-%m-%d' ) as addTime,"+
                'article.view_count as view_count ,'+
                'type.typeName as typeName ,'+
                'type.id as typeId '+
                'FROM article LEFT JOIN type ON article.type_id = type.Id '+
                'WHERE article.id='+id
        
            const result = await app.mysql.query(sql)
            return result
        }catch(error){
            console.log(error)
            return null
        }
    }

}
module.exports = adminService